#pragma once

#include "Vector.h"

Vector::Vector(size_t size)
{
	len = size;
	cap = len * 2;
	if (len == 0)
	{
		cap = 1;
		digit = 0;
	}
	else
	{
		digits = new int64[cap];
		for (size_t i = 0; i < cap; i++)
			digits[i] = 0;
	}
}

Vector::Vector(const Vector& other)
{
	if (&other == this)
		return;
	len = other.len;
	cap = other.cap;
	if (len == 1)
		digit = other.digit;
	if (len > 1)
	{
		digits = new int64[cap];
		for (size_t i = 0; i < cap; i++)
			digits[i] = other.digits[i];
	}
}

Vector& Vector::operator=(const Vector& other)
{
	if (&other == this) return *this;
	if (cap > 1)
		delete[] digits;
	len = other.len;
	cap = other.cap;

	if (len == 1)
		digit = other.digit;
	else
	{
		digits = new int64[cap];
		for (size_t i = 0; i < cap; i++)
			digits[i] = other.digits[i];
	}
	return *this;
}

size_t Vector::size() const
{
	return len;
}

int64& Vector::operator[](size_t index)
{
	if (index >= len)
		resize(index + 1);
	if (len == 1)
		return digit;
	else
		return digits[index];
}

int64 Vector::operator[](size_t index) const
{
	if (index >= len)
		return 0;
	if (len == 1)
		return digit;
	else
		return digits[index];
}

Vector::~Vector()
{
	if (len > 1)
		delete[] digits;
}

void Vector::resize(size_t size)
{
	if (size < cap)
		return;
	size_t new_cap = cap;
	while (size > new_cap);
		new_cap *= 2;
	Vector tmp(new_cap);

	if (len == 1)
		tmp[0] = digit;
	else
	{
      		for (size_t i = 0; i < cap; i++)
			tmp[i] = digits[i];
      		delete[] digits;
	}
	len = size;
	cap = new_cap;
	digits = new_digits;
}